package leetcode;

import java.util.ArrayList;
import java.util.List;

public class ReverseWord {
    public static void main(String[] args) {

        String s ="the sky is  blue";
        String res = reverseWords(s);
        System.out.println(res);
    }

    public static String reverseWords(String s) {
        String[] arr = s.split(" ");
        List<String> list = new ArrayList<>();
        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i] != null && !arr[i].equals("")) {
                list.add(arr[i]);
            }
        }

        String res = String.join(" ",list);
        return res;
    }
}
